package com.woniuxy.huayoung.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.woniuxy.huayoung.entity.Order;
import com.woniuxy.huayoung.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author dushen
 * @className: PayController
 * @description: TODO 类描述
 * @create 2021-07-26-16:34
 */
@RestController
@RequestMapping("/pay")
@Slf4j
public class PayController {

    @Resource
    private OrderService orderService;

    private String APP_ID = "2021000117632593";
    private String APP_PRIVATE_KEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCPgNxs2o20sfZOTjneIJCDxLBHuRlTa7gelx8cWYbVuajqlX3eH91yUNood4lQC2cupIH1r/ZZ677qTgDftHK3J0m5v7TwFiD/kvoXg7UzXsgNB5qtb3W9XiFVLhO6WDhSeeXZBIYenKexfzZ0M8MGEsYq4yZ5+LRjUuUUfsVlqLpUbQMbYTbTkCEVowjj/w95QAj54QQ591H1QovyF7zXtyaExcddF/66vSNqrqV3irDYr3kLo1TguOqCzR35c7t311SehxV4y/0dGhGfrWqmVadPFvC4PAUWoBVY9BnYrbLrmCebyhV6NPegW9YpT6mnvJGAIKxxJr94qnxdSGLnAgMBAAECggEAC6stwUgwN5406f8gY3iAZ8aJkvtxHMBok0w0gmihNyXGn3SSbIpoxk/evWWi67hRppBH5QNu47bwqjq3hXk1zsDKFOmKu+8AtedsoJbcGBY/7BdPM4UZrNFHopaBkih0Q/iXgQMGKN0SIbO3aNGArTC/nmMkEMzxVhY6fGUmhfKIKS+DC/MudgmQuWRKIL0ue2wXW7AgIqzojijZ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AQ8AMIIBCgKCAQEA0KvKPBWhi+M9N6EBJDsfk4iry0tTbyRkCPmKIcbrnGGwQU2RCPRXbOw6o0uOKaynvYz5paeb8kxR3PEn+cdR9HeK2YMvdLuwVRNsFiNbP1TvH3dHVZbAddUDL0BpxQbeR6Zz6lFVl3JaUYquCef4h9wvjw1cjTz5cgBwf5UYlj0s1TtCUORYtZUmMHmGri3UXJtn5L6q/7pyMhDOW+/1L6L8NC0aRl3kFqX0w2iWcP5APNAzfqiNgx1L+b+LxnyO4G586AoQp1pruB6XS2Wv2U6F3VwdZFlwhpGCQr7nmOAd8Wf2GDJYL6wqV5TJ4Tq0yI+cwruupukkjh1w5739LwIDAQAB";
    private String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
    private String FORMAT = "JSON";
    private String SIGN_TYPE = "RSA2";
    private String NOTIFY_URL = "http://ctf7g8t.nat.ipyingshe.com/pay/notifyUrl";
    private String RETURN_URL = "http://dushen.cn1.utools.club/index.jsp";


    @GetMapping("/alipay")
    public String alipay(HttpServletRequest req, HttpServletResponse resp) throws IOException, AlipayApiException {
        String orderNo = req.getParameter("orderNo");

        Order order = orderService.findOrder(orderNo);
        //实例化客户端,填入所需参数
        AlipayClient alipayClient =  new DefaultAlipayClient(GATEWAY_URL,
                APP_ID, APP_PRIVATE_KEY, "json", "utf-8", ALIPAY_PUBLIC_KEY, "RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = orderNo;
        //付款金额，必填
        String total_amount = String.valueOf(order.getMoney());
        //订单名称，必填
        String subject ="书籍";
        //商品描述，可空
        String body = "书籍商品";
        request.setBizContent("{" +
                "    \"out_trade_no\":\"" + out_trade_no + "\"," +//商户订单号
                "    \"total_amount\":\"" + total_amount + "\"," +
                "    \"subject\":\"" + subject + "当面付" + "\"," +
                "    \"store_id\":\"00100\"," + // (必填) 商户门店编号，通过门店号和商家后台可以配置精准到门店的折扣信息，详询支付宝技术支持
                "    \"timeout_express\":\"5m\"}");
        AlipayTradePrecreateResponse response = null;

        //支付宝二维码URL

        response = alipayClient.execute(request);

        String qrCode = response.getQrCode();

        return qrCode;
    }

    @RequestMapping("notifyUrl")
    public String notifyUrl(HttpServletRequest request, HttpServletResponse response){
        String out_trade_no = request.getParameter("out_trade_no");
        log.debug("支付宝异步回调成功"+out_trade_no);
        orderService.updateorder(out_trade_no);
        return "success";
    }
}
